package unsw.dao;

import unsw.model.DeviceFile;
import unsw.model.FileStatus;
import unsw.utils.DatabaseConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DeviceFileDao {

    private static final Connection conn;

    static {
        try {
            conn = DatabaseConnection.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public void addFileToDevice(String deviceId, String filename, String content) {
        int fileSize = content.length();
        String sql = "INSERT INTO devicefile ( filename, data,fileSize,deviceId,status,transferreddata) VALUES (?, ?, ?, ?, ?, ?)";
        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, filename);
            pstmt.setString(2, content);
            pstmt.setInt(3, fileSize);
            // 0 created  1 completed
            pstmt.setString(4, deviceId);
            pstmt.setString(5, FileStatus.CreatedByDevice.name());
            pstmt.setString(6, "");
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public DeviceFile getFileByFileName(String fileName, String deviceId) {
        String sql = "SELECT id, filename, data, fileSize,status,transferreddata FROM devicefile WHERE filename = ? and deviceId = ?";
        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, fileName);
            pstmt.setString(2, deviceId);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    int id = rs.getInt("id");
                    String data = rs.getString("data");
                    int fileSize = rs.getInt("fileSize");
                    String status = rs.getString("status");
                    String transferreddata = rs.getString("transferreddata");
                    return new DeviceFile(id, fileName, data, fileSize, deviceId, status, transferreddata);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public void updateFileStatus(String fileName, String deviceId, FileStatus status) {
        String sql = "UPDATE devicefile SET status= ? WHERE fileName = ? and deviceId = ?";
        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, status.name());
            pstmt.setString(2, fileName);
            pstmt.setString(3, deviceId);
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void updateFileTransferreddata(DeviceFile file, String deviceId, String readyData) {
        String sql = "UPDATE devicefile SET transferreddata= ?,deviceId = ? WHERE fileName = ? and deviceId = ?";
        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            readyData = file.getTransferreddata() + readyData;
            pstmt.setString(1, readyData);
            pstmt.setString(2, deviceId);
            pstmt.setString(3, file.getFilename());
            pstmt.setString(4, file.getDeviceId());
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void deleteFileByDeviceId(String deviceId) {
        String sql = "DELETE FROM devicefile WHERE deviceId = ? ";
        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, deviceId);
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void addOrUpdateFileToDevice(String deviceId, String filename, String content, String receivedData) {
        DeviceFile fileByFileName = getFileByFileName(filename, deviceId);
        int fileSize = content.length();
        if (fileByFileName == null) {
            //如果文件不存在,则新增文件
            String sql = "INSERT INTO devicefile ( filename, data,fileSize,deviceId,status,transferreddata) VALUES (?, ?, ?, ?, ?, ?)";
            try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
                pstmt.setString(1, filename);
                pstmt.setString(2, content);
                pstmt.setInt(3, fileSize);
                pstmt.setString(4, deviceId);
                pstmt.setString(5, FileStatus.TransferringToDevice.name());
                pstmt.setString(6, receivedData);
                pstmt.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } else {
            //如果文件存在,则更新文件
            String sql = "UPDATE devicefile SET transferreddata= concat(transferreddata,?) WHERE filename = ? and deviceId = ?";
            try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
                pstmt.setString(1, receivedData);
                pstmt.setString(2, filename);
                pstmt.setString(3, deviceId);
                pstmt.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


}
